import os, time
from threading import Thread

from connection import MysqlConnection
from message import Message
from message_sender import MessageSender

conn_wrapper = MysqlConnection()
Message.dropTable(ifExists=True)
Message.createTable(ifNotExists=True)

def test_simple():
  remote_host = 'http://localhost:8080'
  sender = MessageSender(remote_host)
    
  # send message
  content = 'Simple: I am l33t H4X0R @#$$@%@%^ $%@^%$&^*&(){}:L:>< '
  message = sender.send(content)
  
  # test perimeters
  assert message != None
  assert message.content == content
  assert message.status == Message.STATUS_SUCCESS
  
  messages_resultset = Message.selectBy( content= 'Simple:%', content = content )
  
  # There are 2 messages: 1 for outgoing, 1 for incoming
  # Why? Because I use the same database for both sender and receiver
  assert 2 == messages_resultset.count()
  assert messages_resultset[0].content == messages_resultset[1].content
  assert messages_resultset[0].direction != messages_resultset[1].direction

def test_sender_daemon():
  content = 'Daemon Test: '
  for i in xrange(10):   
    message = Message.create(content + str(time.time()), Message.DIRECTION_OUT)
  
  #run daemon  
  os.system("../bin/sender_daemon.py http://localhost:8080 restart")
  
  assert 0 == Message.selectBy(content= 'Daemon Test:%', status = Message.STATUS_NONE).count()
  assert 0 == Message.selectBy(content= 'Daemon Test:%', status = Message.STATUS_FAILED).count()

'''
Simple threading setup for test_stress
'''
class TheThread(Thread):
  def __init__(self, sender, content, assertion=True):
    Thread.__init__(self)
    self.sender = sender
    self.content = content
    self.assertion = assertion
    
  def run(self):
    message = self.sender.send(self.content)
    
    if self.assertion:
      assert message != None
      assert message.content == self.content
      assert message.status == Message.STATUS_SUCCESS
  
def test_stress():
  remote_host = 'http://localhost:8080'
  content = 'Stress: I am l33t H4X0R @#$$@%@%^ $%@^%$&^*& '
  
  MessageSender.control_daemon(remote_host, 'stop')
  sender = MessageSender(remote_host)

  num_threads = 500
  for i in xrange(num_threads):
    current = TheThread(sender, content + str(time.time()))
    current.start()
      
  messages_out_resultset = Message.selectBy( content= 'Stress:%', direction = Message.DIRECTION_OUT )
  messages_in_resultset = Message.selectBy( content= 'Stress:%', direction = Message.DIRECTION_IN )
  
  total_out = messages_out_resultset.count()
  total_in = messages_in_resultset.count()
  assert total_out == total_in

# Commented becaus it's too much, taxing the CPU.  
# def test_stress_2():
#   remote_host = 'http://localhost:8080'
#   content = 'Mega Stress: ' + ('abc ' * 200)
#   
#   MessageSender.control_daemon(remote_host, 'stop')
#   sender = MessageSender(remote_host)
# 
#   num_threads = 10000
#   for i in xrange(num_threads):
#     current = TheThread(sender, content + str(time.time()), assertion=False)
#     current.start()
#       
#   messages_out_resultset = Message.selectBy( content= 'Mega Stress:%', direction = Message.DIRECTION_OUT )
#   messages_in_resultset = Message.selectBy( content= 'Mega Stress:%', direction = Message.DIRECTION_IN )
#   
#   total_out = messages_out_resultset.count()
#   total_in = messages_in_resultset.count()
#   assert total_out == total_in